<!DOCTYPE html>
<html lang="zh_CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"dengmin.gitee.io","root":"/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="在项目中安装和配置webpack1 运行 npm i webpack webpack-cli -D 安装webpack相关的包 2 在项目中创建配置文件 webpack.config.js 123module.exports &#x3D; &amp;#123;	mode : &amp;#x27;development&amp;#x27;  &#x2F;&#x2F;production&amp;#125;  3 在package.json 配置文件的scrip">
<meta property="og:type" content="article">
<meta property="og:title" content="webpack基本使用">
<meta property="og:url" content="http://dengmin.gitee.io/2021/04/09/webpack-basic-use/index.html">
<meta property="og:site_name" content="老邓头">
<meta property="og:description" content="在项目中安装和配置webpack1 运行 npm i webpack webpack-cli -D 安装webpack相关的包 2 在项目中创建配置文件 webpack.config.js 123module.exports &#x3D; &amp;#123;	mode : &amp;#x27;development&amp;#x27;  &#x2F;&#x2F;production&amp;#125;  3 在package.json 配置文件的scrip">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-04-09T04:45:51.000Z">
<meta property="article:modified_time" content="2021-04-10T08:47:58.479Z">
<meta property="article:author" content="老邓">
<meta property="article:tag" content="webpack">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://dengmin.gitee.io/2021/04/09/webpack-basic-use/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh_CN'
  };
</script>

  <title>webpack基本使用 | 老邓头</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">老邓头</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh_CN">
    <link itemprop="mainEntityOfPage" href="http://dengmin.gitee.io/2021/04/09/webpack-basic-use/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="老邓">
      <meta itemprop="description" content="希望是美好的，而美好的事物永不消逝">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="老邓头">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          webpack基本使用
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>

              <time title="Created: 2021-04-09 12:45:51" itemprop="dateCreated datePublished" datetime="2021-04-09T12:45:51+08:00">2021-04-09</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">Edited on</span>
                <time title="Modified: 2021-04-10 16:47:58" itemprop="dateModified" datetime="2021-04-10T16:47:58+08:00">2021-04-10</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/webpack/" itemprop="url" rel="index"><span itemprop="name">webpack</span></a>
                </span>
            </span>

          
            <span id="/2021/04/09/webpack-basic-use/" class="post-meta-item leancloud_visitors" data-flag-title="webpack基本使用" title="Views">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">Views: </span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine: </span>
    
    <a title="valine" href="/2021/04/09/webpack-basic-use/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2021/04/09/webpack-basic-use/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h3 id="在项目中安装和配置webpack"><a href="#在项目中安装和配置webpack" class="headerlink" title="在项目中安装和配置webpack"></a>在项目中安装和配置webpack</h3><p>1 运行 npm i <code>webpack webpack-cli</code> -D 安装webpack相关的包</p>
<p>2 在项目中创建配置文件 <code>webpack.config.js</code></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">	mode : <span class="string">&#x27;development&#x27;</span>  <span class="comment">//production</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>3 在package.json 配置文件的scripts 中新增<code>dev</code>脚本</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&quot;script&quot;: &#123;</span><br><span class="line">	&quot;dev&quot;: &quot;webpack&quot;      &#x2F;&#x2F;可以通过npm run dev运行</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>4 在终端运行 <code>npm run dev</code>运行webpack进行项目打包</p>
<h3 id="配置打包的入口与出口"><a href="#配置打包的入口与出口" class="headerlink" title="配置打包的入口与出口"></a>配置打包的入口与出口</h3><p>Webpack 的4.x版本总默认约定</p>
<ul>
<li>打包的入口文件为 src -&gt; index.js</li>
<li>打包的出口文件 dist -&gt; main.js</li>
</ul>
<p>自定义入口和出口</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">&#x27;path&#x27;</span>)</span><br><span class="line"><span class="built_in">module</span>.exports= &#123;</span><br><span class="line">    mode: <span class="string">&quot;development&quot;</span>,</span><br><span class="line">    entry: path.join(__dirname, <span class="string">&#x27;./src/index.js&#x27;</span>),  <span class="comment">//打包的入口文件路径</span></span><br><span class="line">    output: &#123;</span><br><span class="line">        path: path.join(__dirname, <span class="string">&#x27;dist&#x27;</span>),  <span class="comment">//输出我的文件存放路径</span></span><br><span class="line">        filename: <span class="string">&quot;bundle.js&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="配置webpack自动打包"><a href="#配置webpack自动打包" class="headerlink" title="配置webpack自动打包"></a>配置webpack自动打包</h3><ol>
<li>运行npm i <code>webpack-dev-server</code> -D </li>
<li>修改package.json 中的scripts </li>
</ol>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;scripts&quot;</span>: &#123;</span><br><span class="line">  <span class="string">&quot;dev&quot;</span>: <span class="string">&quot;webpack-dev-server&quot;</span>  <span class="comment">//webpack 替换成 webpack-dev-server</span></span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>

<ol start="3">
<li>把src/index.html 中script的src改成/bundle.js</li>
</ol>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;/bundle.js&quot;</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<ol start="4">
<li>运行<code>npm run dev</code></li>
</ol>
<blockquote>
<p><code>webpack-dev-server</code>打包生成的文件默认放到了项目的根目录中,而且是虚拟看不见的</p>
</blockquote>
<h3 id="生成预览页面"><a href="#生成预览页面" class="headerlink" title="生成预览页面"></a>生成预览页面</h3><ol>
<li>运行npm i <code>html-webpack-plugin</code> -D</li>
<li>修改webpack.config.js 添加配置</li>
</ol>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">&#x27;path&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> HtmlWebpackPlugin = <span class="built_in">require</span>(<span class="string">&#x27;html-webpack-plugin&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> htmlPlugin = <span class="keyword">new</span> HtmlWebpackPlugin(&#123;</span><br><span class="line">    template: <span class="string">&#x27;./src/index.html&#x27;</span>,</span><br><span class="line">    filename: <span class="string">&#x27;index.html&#x27;</span></span><br><span class="line">&#125;);</span><br><span class="line"><span class="built_in">module</span>.exports= &#123;</span><br><span class="line">    mode: <span class="string">&quot;development&quot;</span>,</span><br><span class="line">    entry: path.join(__dirname, <span class="string">&#x27;./src/index.js&#x27;</span>),</span><br><span class="line">    output: &#123;</span><br><span class="line">        path: path.join(__dirname, <span class="string">&#x27;dist&#x27;</span>),</span><br><span class="line">        filename: <span class="string">&quot;bundle.js&quot;</span></span><br><span class="line">    &#125;,</span><br><span class="line">    plugins: [htmlPlugin]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="打包处理css样式文件"><a href="#打包处理css样式文件" class="headerlink" title="打包处理css样式文件"></a>打包处理css样式文件</h3><p>1    运行 npm i <code>style-loader css-loader</code> -D 安装处理css文件的loader</p>
<p>2    在webpack.config.js 的<code>module</code> -&gt; <code>rules</code>数组中,添加loader规则</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">module: &#123;</span><br><span class="line">	rules: [</span><br><span class="line">		&#123;test: /\.css$/, use: [&#x27;style-loader&#x27;, &#x27;css-loader&#x27;]&#125;</span><br><span class="line">	]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="打包处理less文件"><a href="#打包处理less文件" class="headerlink" title="打包处理less文件"></a>打包处理less文件</h3><ol>
<li>运行 npm i <code>less-loader less</code> -D</li>
<li>在webpack.config.js中 添加规则</li>
</ol>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">module: &#123;</span><br><span class="line">	rules: [</span><br><span class="line">		&#123;test: /\.css$/, use: [&#x27;style-loader&#x27;, &#x27;css-loader&#x27;]&#125;,</span><br><span class="line">    &#123;test: /\.less$/, use: [&#x27;style-loader&#x27;, &#x27;css-loader&#x27;, &#x27;less-loader&#x27;]&#125;</span><br><span class="line">	]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="打包处理scss文件"><a href="#打包处理scss文件" class="headerlink" title="打包处理scss文件"></a>打包处理scss文件</h3><ol>
<li>运行npm i <code>sass-loader node-sass</code> -D</li>
<li>在webpack.config.js中 添加规则</li>
</ol>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">module: &#123;</span><br><span class="line">	rules: [</span><br><span class="line">		&#123;test: /\.css$/, use: [&#x27;style-loader&#x27;, &#x27;css-loader&#x27;]&#125;,</span><br><span class="line">    &#123;test: /\.less$/, use: [&#x27;style-loader&#x27;, &#x27;css-loader&#x27;, &#x27;less-loader&#x27;]&#125;</span><br><span class="line">	]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="配置postCSS-自动添加css的兼容前缀"><a href="#配置postCSS-自动添加css的兼容前缀" class="headerlink" title="配置postCSS 自动添加css的兼容前缀"></a>配置postCSS 自动添加css的兼容前缀</h3><ol>
<li>运行 npm i <code>postcss-loader autoprefixer</code> -D</li>
<li>在项目根目录中创建postCSS的配置文件postcss.config.js</li>
</ol>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> autoprefixer = <span class="built_in">require</span>(<span class="string">&#x27;autoprefixer&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">	plugin: [autoprefixer]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ol start="3">
<li>在webpack.config.js文件中 修改css 的loader</li>
</ol>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">module: &#123;</span><br><span class="line">	rules: [</span><br><span class="line">		&#123;test: /\.css$/, use: [&#x27;style-loader&#x27;, &#x27;css-loader&#x27;, &#x27;postcss-loader&#x27;]&#125;,  //增加postcss-loader</span><br><span class="line">		&#123;test: /\.less$/, use: [&#x27;style-loader&#x27;, &#x27;css-loader&#x27;, &#x27;less-loader&#x27;]&#125;,</span><br><span class="line">		&#123;test: /\.scss$/, use: [&#x27;style-loader&#x27;, &#x27;css-loader&#x27;, &#x27;sass-loader&#x27;]&#125;</span><br><span class="line">	]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="打包样式标准的图片和字体文件"><a href="#打包样式标准的图片和字体文件" class="headerlink" title="打包样式标准的图片和字体文件"></a>打包样式标准的图片和字体文件</h3><ol>
<li>运行npm i <code>url-loader file-loader</code> -D</li>
<li>在webpack.config.js 的module -&gt; rules中在增加规则</li>
</ol>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">module: &#123;</span><br><span class="line">        rules: [</span><br><span class="line">            &#123;test: /\.css$/, use: [&#x27;style-loader&#x27;, &#x27;css-loader&#x27;, &#x27;postcss-loader&#x27;]&#125;,</span><br><span class="line">            &#123;test: /\.less$/, use: [&#x27;style-loader&#x27;, &#x27;css-loader&#x27;, &#x27;less-loader&#x27;]&#125;,</span><br><span class="line">            &#123;test: /\.scss$/, use: [&#x27;style-loader&#x27;, &#x27;css-loader&#x27;, &#x27;sass-loader&#x27;]&#125;,</span><br><span class="line">            &#123;test: /\.jpg|png|gif|bmp|tff|eot|svg|woff|woff2$/, use: [&#x27;url-loader?limit=16940&#x27;]&#125;</span><br><span class="line">        ]</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<p>其中 url-loader 之后的<code>?</code>是参数项</p>
<blockquote>
<p>limit    用来指定图片的文件大小 单位是字节, 只有小于limit的大小图片会被转化成base64图片</p>
</blockquote>
<h3 id="打包处理js中的高级语法"><a href="#打包处理js中的高级语法" class="headerlink" title="打包处理js中的高级语法"></a>打包处理js中的高级语法</h3><ol>
<li>安装babel转换器相关的包 npm i <code>babel-loader @babel/core @babel/runtime</code> -D</li>
<li>安装babel语法插件相关的包 npm i <code>@babel/preset-env @babel/plugin-transform-runtime @babel/plugin-proposal-class-properties</code> -D</li>
<li>在项目的根目录中创建babel的配置文件 <code>babel.config.js</code> 并初始化</li>
</ol>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">module.exports = &#123;</span><br><span class="line">    presets: [&#x27;@babel/preset-env&#x27;],</span><br><span class="line">    plugins: [&#x27;@babel/plugin-transform-runtime&#x27;, &#x27;@babel/plugin-proposal-class-properties&#x27;]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ol start="4">
<li>在<code>webpack.config.js</code> 的module -&gt; rules 中新增规则</li>
</ol>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&#123;<span class="attr">test</span>: <span class="regexp">/\.js$/</span>, use: [<span class="string">&#x27;babel-loader&#x27;</span>], <span class="attr">exclude</span>: <span class="regexp">/node_modules/</span>&#125;</span><br></pre></td></tr></table></figure>

<h3 id="打包vue文件"><a href="#打包vue文件" class="headerlink" title="打包vue文件"></a>打包vue文件</h3><ol>
<li><p>安装依赖包 npm  i <code>vue-loader@15.9.2 vue-template-compiler</code> -D</p>
</li>
<li><p>在webpack.config.js 中配置</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> VueLoaderPlugin = <span class="built_in">require</span>(<span class="string">&#x27;vue-loader/lib/plugin&#x27;</span>)</span><br><span class="line">&#123;</span><br><span class="line">plugins: [htmlPlugin, <span class="keyword">new</span> VueLoaderPlugin()],</span><br><span class="line"><span class="built_in">module</span>: &#123;</span><br><span class="line">	rules: [</span><br><span class="line">      &#123;<span class="attr">test</span>: <span class="regexp">/\.css$/</span>, use: [<span class="string">&#x27;style-loader&#x27;</span>, <span class="string">&#x27;css-loader&#x27;</span>, <span class="string">&#x27;postcss-loader&#x27;</span>]&#125;,</span><br><span class="line">      &#123;<span class="attr">test</span>: <span class="regexp">/\.less$/</span>, use: [<span class="string">&#x27;style-loader&#x27;</span>, <span class="string">&#x27;css-loader&#x27;</span>, <span class="string">&#x27;less-loader&#x27;</span>]&#125;,</span><br><span class="line">      &#123;<span class="attr">test</span>: <span class="regexp">/\.scss$/</span>, use: [<span class="string">&#x27;style-loader&#x27;</span>, <span class="string">&#x27;css-loader&#x27;</span>, <span class="string">&#x27;sass-loader&#x27;</span>]&#125;,</span><br><span class="line">      &#123;<span class="attr">test</span>: <span class="regexp">/\.jpg|png|gif|bmp|tff|eot|svg|woff|woff2$/</span>, use: [<span class="string">&#x27;url-loader?limit=16940&#x27;</span>]&#125;,</span><br><span class="line">      &#123;<span class="attr">test</span>: <span class="regexp">/\.js$/</span>, use: [<span class="string">&#x27;babel-loader&#x27;</span>], <span class="attr">exclude</span>: <span class="regexp">/node_modules/</span>&#125;,</span><br><span class="line">      &#123;<span class="attr">test</span>: <span class="regexp">/\.vue/</span>, use: [<span class="string">&#x27;vue-loader&#x27;</span>]&#125;</span><br><span class="line">    ]</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ol>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/webpack/" rel="tag"># webpack</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/03/12/k8s_addOrDelete_node/" rel="prev" title="k8s集群删除和添加node节点">
      <i class="fa fa-chevron-left"></i> k8s集群删除和添加node节点
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/04/10/tomcatNIO2/" rel="next" title="springboot tomcat启用Http11Nio2Protocol">
      springboot tomcat启用Http11Nio2Protocol <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9C%A8%E9%A1%B9%E7%9B%AE%E4%B8%AD%E5%AE%89%E8%A3%85%E5%92%8C%E9%85%8D%E7%BD%AEwebpack"><span class="nav-number">1.</span> <span class="nav-text">在项目中安装和配置webpack</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%85%8D%E7%BD%AE%E6%89%93%E5%8C%85%E7%9A%84%E5%85%A5%E5%8F%A3%E4%B8%8E%E5%87%BA%E5%8F%A3"><span class="nav-number">2.</span> <span class="nav-text">配置打包的入口与出口</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%85%8D%E7%BD%AEwebpack%E8%87%AA%E5%8A%A8%E6%89%93%E5%8C%85"><span class="nav-number">3.</span> <span class="nav-text">配置webpack自动打包</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%94%9F%E6%88%90%E9%A2%84%E8%A7%88%E9%A1%B5%E9%9D%A2"><span class="nav-number">4.</span> <span class="nav-text">生成预览页面</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%89%93%E5%8C%85%E5%A4%84%E7%90%86css%E6%A0%B7%E5%BC%8F%E6%96%87%E4%BB%B6"><span class="nav-number">5.</span> <span class="nav-text">打包处理css样式文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%89%93%E5%8C%85%E5%A4%84%E7%90%86less%E6%96%87%E4%BB%B6"><span class="nav-number">6.</span> <span class="nav-text">打包处理less文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%89%93%E5%8C%85%E5%A4%84%E7%90%86scss%E6%96%87%E4%BB%B6"><span class="nav-number">7.</span> <span class="nav-text">打包处理scss文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%85%8D%E7%BD%AEpostCSS-%E8%87%AA%E5%8A%A8%E6%B7%BB%E5%8A%A0css%E7%9A%84%E5%85%BC%E5%AE%B9%E5%89%8D%E7%BC%80"><span class="nav-number">8.</span> <span class="nav-text">配置postCSS 自动添加css的兼容前缀</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%89%93%E5%8C%85%E6%A0%B7%E5%BC%8F%E6%A0%87%E5%87%86%E7%9A%84%E5%9B%BE%E7%89%87%E5%92%8C%E5%AD%97%E4%BD%93%E6%96%87%E4%BB%B6"><span class="nav-number">9.</span> <span class="nav-text">打包样式标准的图片和字体文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%89%93%E5%8C%85%E5%A4%84%E7%90%86js%E4%B8%AD%E7%9A%84%E9%AB%98%E7%BA%A7%E8%AF%AD%E6%B3%95"><span class="nav-number">10.</span> <span class="nav-text">打包处理js中的高级语法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%89%93%E5%8C%85vue%E6%96%87%E4%BB%B6"><span class="nav-number">11.</span> <span class="nav-text">打包vue文件</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">老邓</p>
  <div class="site-description" itemprop="description">希望是美好的，而美好的事物永不消逝</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">5</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
        <span class="site-state-item-count">3</span>
        <span class="site-state-item-name">categories</span>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">5</span>
        <span class="site-state-item-name">tags</span>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">老邓</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a>
  </div>

        






<script>
  (function() {
    function leancloudSelector(url) {
      url = encodeURI(url);
      return document.getElementById(url).querySelector('.leancloud-visitors-count');
    }

    function addCount(Counter) {
      var visitors = document.querySelector('.leancloud_visitors');
      var url = decodeURI(visitors.id);
      var title = visitors.dataset.flagTitle;

      Counter('get', '/classes/Counter?where=' + encodeURIComponent(JSON.stringify({ url })))
        .then(response => response.json())
        .then(({ results }) => {
          if (results.length > 0) {
            var counter = results[0];
            leancloudSelector(url).innerText = counter.time + 1;
            Counter('put', '/classes/Counter/' + counter.objectId, { time: { '__op': 'Increment', 'amount': 1 } })
              .catch(error => {
                console.error('Failed to save visitor count', error);
              });
          } else {
              Counter('post', '/classes/Counter', { title, url, time: 1 })
                .then(response => response.json())
                .then(() => {
                  leancloudSelector(url).innerText = 1;
                })
                .catch(error => {
                  console.error('Failed to create', error);
                });
          }
        })
        .catch(error => {
          console.error('LeanCloud Counter Error', error);
        });
    }

    function showTime(Counter) {
      var visitors = document.querySelectorAll('.leancloud_visitors');
      var entries = [...visitors].map(element => {
        return decodeURI(element.id);
      });

      Counter('get', '/classes/Counter?where=' + encodeURIComponent(JSON.stringify({ url: { '$in': entries } })))
        .then(response => response.json())
        .then(({ results }) => {
          for (let url of entries) {
            let target = results.find(item => item.url === url);
            leancloudSelector(url).innerText = target ? target.time : 0;
          }
        })
        .catch(error => {
          console.error('LeanCloud Counter Error', error);
        });
    }

    let { app_id, app_key, server_url } = {"enable":true,"app_id":"EBgSzmhfH3OYpuwKsg7zdgaa-gzGzoHsz","app_key":"1B6eSw1fKwO8hBtpRs6I5uyQ","server_url":"https://ebgszmhf.lc-cn-n1-shared.com","security":false};
    function fetchData(api_server) {
      var Counter = (method, url, data) => {
        return fetch(`${api_server}/1.1${url}`, {
          method,
          headers: {
            'X-LC-Id'     : app_id,
            'X-LC-Key'    : app_key,
            'Content-Type': 'application/json',
          },
          body: JSON.stringify(data)
        });
      };
      if (CONFIG.page.isPost) {
        if (CONFIG.hostname !== location.hostname) return;
        addCount(Counter);
      } else if (document.querySelectorAll('.post-title-link').length >= 1) {
        showTime(Counter);
      }
    }

    let api_server = app_id.slice(-9) !== '-MdYXbMMI' ? server_url : `https://${app_id.slice(0, 8).toLowerCase()}.api.lncldglobal.com`;

    if (api_server) {
      fetchData(api_server);
    } else {
      fetch('https://app-router.leancloud.cn/2/route?appId=' + app_id)
        .then(response => response.json())
        .then(({ api_server }) => {
          fetchData('https://' + api_server);
        });
    }
  })();
</script>


      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : true,
      appId      : 'EBgSzmhfH3OYpuwKsg7zdgaa-gzGzoHsz',
      appKey     : '1B6eSw1fKwO8hBtpRs6I5uyQ',
      placeholder: "Just go go",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : false,
      lang       : 'zh-cn' || 'zh-cn',
      path       : location.pathname,
      recordIP   : true,
      serverURLs : 'https://ebgszmhf.lc-cn-n1-shared.com'
    });
  }, window.Valine);
});
</script>

</body>
</html>
